package com.bike.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.bike.controller.BaseController;
import com.bike.entity.SysMenu;
import com.bike.entity.SysRole;
import com.bike.service.impl.SysMenuServiceImpl;
import com.bike.service.impl.SystemServiceImpl;
import com.bike.util.PageData;
import com.bike.util.RightsHelper;

@Controller
@RequestMapping("index")
public class IndexController extends BaseController{
	 
	@Autowired
	private SystemServiceImpl systemService;
	@Autowired
	private SysMenuServiceImpl sysMenuServiceImpl;
 
	/**
	 * 进入首页
	 * @return
	 */
	@RequestMapping("index")
	public String index(HttpServletRequest request,Model model){
		PageData pd=PageData.getPageDataByPage(request);
		SysRole role=(SysRole) request.getSession().getAttribute("role");
		pd.put("roleId", role.getRoleId());
		pd.put("notAll", "notAll");
		model.addAttribute("menuTree", JSONArray.toJSONString(sysMenuServiceImpl.selectBySysMenuTree(pd)));
		return "/system/index/index";
	}
	/**
	 * 根据父id获取菜单集合
	 * @param parentId
	 * @return
	 */
	@RequestMapping("menu")
	@ResponseBody
	public List<SysMenu> menu(HttpServletRequest request,Model model){
		PageData pd=PageData.getPageDataByPage(request);
		long parentId=pd.getLong("parentId");
		long roleId=pd.getLong("roleId");
		if(roleId==-1){
			return new ArrayList<>();
		}else if(roleId==0){
			SysRole role=(SysRole) request.getSession().getAttribute("role");
			List<SysMenu> menuList=systemService.selectMenuByParentId(role.getMenuRights(),parentId);
			return  menuList;
		}else{
			List<SysMenu> menuList=sysMenuServiceImpl.selectBySysMenuTree(pd);
			return menuList ;
		}
		
	}
 
	/**
	 * 删除
	 * @param parentId
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Map<String, Object>  delete(long menuId){
		systemService.deleteMenu(menuId);
		return getSuccessMap() ;
	}
 
 
}
